import java.util.Arrays;

/**
 * Created by xiebang
 * Description
 * User:小谢
 * Date:2021-08-04
 * Time:9:50
 */
public class MyArrayList {
    public int[] elem;//elem = null
    public int usedSize;
    public static int capacity = 10;
    public  MyArrayList() {
        this.elem = new int[capacity];
    }

        // 打印顺序表
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }
        // 在 pos 位置新增元素
        //在pos位置之前是有数据的
        //满的情况

    public boolean isFull() {
//            if(this.elem.length==this.usedSize) {
//                return true;
//            }
//            return false;
        return this.usedSize == capacity;
    }

    public void add(int pos, int data) {

        if(pos <0 ||pos > usedSize) {//坐标非法
             System.out.println("插入位置非法");
             return;
        }

        if(isFull()) {
            this.elem = Arrays.copyOf(this.elem,2*capacity);
            capacity *= 2;
        }

        for(int i = this.usedSize - 1; i >=pos; i--) {
            elem[i+1] = elem[i];
        }
            elem[pos] = data;
            this.usedSize++;
    }
    public boolean isEmpty() {
//               if(this.usedSize == 0) {
//                return true;
//               }
//                return false;
        return this.usedSize == 0;
    }

        // 判定是否包含某个元素
    public boolean contains(int toFind) {
        if(!this.isEmpty()) {
            for (int i = 0; i < this.usedSize; i++) {
                if (this.elem[i] == toFind) {
                    return true;
                }
            }
        }
        return false;
    }

    // 查找某个元素对应的位置
    public int search(int toFind) {
        if(this.contains(toFind)) {
            for (int i = 0; i < this.usedSize; i++) {
                if(this.elem[i] == toFind) {
                    return i;
                }
            }
        }
        return -1;
    }

    // 获取 pos 位置的元素
    public int getPos(int pos) {
        if (isEmpty()){
            throw  new RuntimeException("顺序表是空的");
        }

        if(pos < 0 || pos >= this.usedSize) {
            throw new RuntimeException("pos不合法");
        }

        return this.elem[pos];
    }

    // 获取顺序表长度
    public int size() {
        return this.usedSize;
    }

    // 给 pos 位置的元素设为 value
    public void setPos(int pos, int value) {
        if(isEmpty()) {
            System.out.println("顺序表为空！");
            return;
        }

        if(pos < 0 || pos >= this.usedSize) {
            System.out.println("pos不合法");
        }

        this.elem[pos] = value;
    }
    //删除第一次出现的关键字key
    public void remove(int toRemove) {
            if (isEmpty()) {
                System.out.println("链表为空");
                return;
            }
            int pos = this.search(toRemove);

            if(pos == -1) {
                System.out.println("没有这个数字");
                return;
            }else  {
                for (int i = pos; i <this.usedSize - 1;i++) {
                    this.elem[i] = this.elem[i+1];
                }

                this.usedSize--;
            }
    }
    // 清空顺序表
    public void clear() {
        for (int i = 0; i < this.usedSize; i++) {
            this.elem[i] = 0;
        }
        this.usedSize = 0;
        }
}
//public class MyArrayList {
//    public int[] elem;//elem = null
//    public int usedSize;
//   // public static final int capacity = 10;
//    public  MyArrayList(int capacity) {
//        this.elem = new int[capacity];
//
//    }
//}